﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace S_Gridding.Infrastructure
{
    /// <summary>
    /// 基础数据的仓储实现
    /// </summary>
    public class BaseRepository<T> : IRepository<T> where T : class, new()
    {
        /// <summary>
        /// 需要下载SqlSugarcore 包 相当于EF的注入上下文类
        /// </summary>
        private readonly ISqlSugarClient _client;

        public BaseRepository(ISqlSugarClient client)
        {
            _client = client;
        }


        /// <summary>
        /// SqlSugar添加信息
        /// </summary>
        /// <param name="t">添加的实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddAsync(T t)
        {
            var res = await _client.Insertable(t).ExecuteCommandAsync();
            return res;
        }

        /// <summary>
        /// SqlSugar 单个真删除
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(T t)
        {
            var res = await _client.Deleteable(t).ExecuteCommandAsync();
            return res;
        }

        /// <summary>
        ///  SqlSugar 修改信息
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(T t)
        {
            var res=await _client.Updateable(t).ExecuteCommandAsync();
            return res;
        }

        /// <summary>
        ///  SqlSugar反填  根据主键找到对应的单个对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T> GetFindAsync(int id)
        {
            return await _client.Queryable<T>().InSingleAsync(id);
        }
    }
}
